package com.shiguiwu.springmybatis.javabase.thread;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @description: 并发限流控制
 * @author: stone
 * @date: Created by 2021/6/21 15:06
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.thread
 */
public class SemaphoreTests {

    private static final Semaphore SEMAPHORE = new Semaphore(2);


    public static void main(String[] args) {
        testSemaphoreTimeout();
    }


    //todo 中断之后许可可能会加多====================
    public static void testSemaphore() {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    SEMAPHORE.acquire();
                    System.out.println("获取许可=========>" + Thread.currentThread().getName());

                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                    SEMAPHORE.release();
                    System.out.println("释放许可==========>" + Thread.currentThread().getName());

                }
            },"t" + i).start();
        }
    }


    //todo 解决中断之后许可可能会加多====================
    public static void testSemaphore1() {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                boolean flag = false;
                try {
                    SEMAPHORE.acquire();
                    flag = true;
                    System.out.println("获取许可=========>" + Thread.currentThread().getName());

                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                    if (flag) {
                        SEMAPHORE.release();
                        System.out.println("释放许可==========>" + Thread.currentThread().getName());
                    }


                }
            },"t" + i).start();
        }
    }

    //todo 解决中断之后许可可能会加多====================
    public static void testSemaphoreTimeout() {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                boolean tryAcquire = false;
                try {
                    tryAcquire = SEMAPHORE.tryAcquire(1, TimeUnit.SECONDS);
                    if (tryAcquire) {
                        System.out.println("获取许可=========>" + Thread.currentThread().getName());

                        TimeUnit.SECONDS.sleep(3);
                    }
                    else {
                        System.out.println("获取许可失败=========>" + Thread.currentThread().getName());
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                    if (tryAcquire) {
                        SEMAPHORE.release();
                        System.out.println("释放许可==========>" + Thread.currentThread().getName());
                    }


                }
            },"t" + i).start();
        }
    }
}
